package cn.cyber.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import cn.cyber.entity.Student;
import cn.cyber.service.StudentService;

//View + Controller
public class Main {
	public static void main(String[] args) {
		// 创建对象
		Student tangSeng = new Student("01", "唐僧", 22, "男", "normal", 100, "软件工程");
		Student sunWuKong = new Student("02", "孙悟空", 22, "男", "normal", 100, "软件工程");
		Student zhuBaJie = new Student("03", "猪八戒", 22, "男", "normal", 100, "软件工程");
		Student shaSeng = new Student("04", "沙僧", 22, "男", "normal", 100, "软件工程");
		List<Student> students = new ArrayList<>();
		students.add(tangSeng);
		students.add(sunWuKong);
		students.add(zhuBaJie);
		students.add(shaSeng);
		// 元素是否在集合中（默认false）
		boolean isInArray = false;
		// 用户选择
        int choice = 0;
        String num = null;
        
        StudentService service = new StudentService();
        
        // 把 list 集合中的元素持久化（序列化，即写入）到文件中
        String filePathName = "src\\data.txt";
        int count = service.init(filePathName, students);
        
        Scanner scanner = new Scanner(System.in);
		do {
			System.out.println("*****************************************************");
            System.out.println("************* Please Choose Your Action: ************");
            System.out.println("**** 1.show all     2.add user     3.delete user ****");
            System.out.println("**** 4.lock user    5.unlock user  6.exit        ****");
            System.out.println("*****************************************************");
            choice = scanner.nextInt();
            switch (choice) {
			case 1:
				service.show(filePathName, count);
				break;
			case 2:
				System.out.println("Please input a new number：");
				num = scanner.next();
				
				isInArray = false;
				// 判断要添加的学号是否在集合中
				for (Student student : students) {
					if (student != null) {
						if (num.equals(student.getNum())) {
							isInArray = true;
							break;
						}
					}
				}
				// 如果用户输入的学号不在集合中,则可以继续执行添加流程
				if (!isInArray) {
					System.out.println("Please input name：");
					String name = scanner.next();
					System.out.println("Please input age：");
					int age = scanner.nextInt();
					System.out.println("Please input gender：");
					String gender = scanner.next();
					System.out.println("Please input score：");
					int score = scanner.nextInt();
					System.out.println("Please input profession：");
					String profession = scanner.next();
					// 根据用户输入的信息创建一个新对象
					Student student = new Student(num, name, age, gender, "normal", score, profession);
			        // 更新 students 集合
					count = service.add(filePathName, student, count);
				} else {
					System.out.println("The number you want to add is already in the system!");
				}
				break;
			case 3:
				System.out.println("Please input the number you want to delete：");
				num = scanner.next();
				
				// 判断要删除的学号是否在数组中
				for (Student student : students) {
					if (num.equals(student.getNum())) {
						students = service.del(num, students);
						break;
					}
				}
				break;
			case 4:
				System.out.println("Please input the num you want to lock:");
				num = scanner.next();
				
				isInArray = false;
				// 判断要锁定的学号是否在集合中
				for (Student student : students) {
					if (student != null) {
						if (num.equals(student.getNum())) {
							isInArray = true;
							student.setStatus("locked");
							break;
						}
					}
				}
				if (!isInArray) {
					System.out.println("The num you want to lock is not in the system!");
				}
				break;
			case 5:
				System.out.println("Please input the num you want to unlock:");
				num = scanner.next();
				
				isInArray = false;
				// 判断要解锁的学号是否在集合中
				for (Student student : students) {
					if (student != null) {
						if (num.equals(student.getNum())) {
							isInArray = true;
							student.setStatus("normal");
							break;
						}
					}
				}
				if (!isInArray) {
					System.out.println("The num you want to unlock is not in the system!");
				}
				break;
			case 6:
				System.out.println("You choose \"exit\"!Bye Bye!");
				break;
			default:
				System.out.println("Please input 1~6!");
				break;
			}
		} while (choice != 6);
		scanner.close();
	}
}
